package com.boot.proofing.controller.base;/**
 * @description
 * @autor xbwu on 2017/8/17.
 */

import com.boot.core.common.base.ResultWapper;
import com.boot.core.common.util.LocaleMessageSource;
import com.boot.core.common.util.UUIDGeneratorUtil;
import com.boot.proofing.common.config.BaseConstant;
import com.boot.proofing.common.util.FileUtil;
import com.boot.proofing.mpweb.base.entity.User;
import com.boot.proofing.mpweb.base.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;

/**
 * 基础控制器
 * 在控制器中可以直接书写参数的校验规则和返回消息
 * @author xbwu
 * @create 2017-08-17
 **/
@Validated
public abstract class BaseController {

    public final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 国际化消息
     */
    @Autowired
    private LocaleMessageSource i18n;
    @Autowired
    IUserService userService;

    public ModelAndView getMV(String viewName) {
        //返回的view就是templetes下面文件的名称
        return new ModelAndView(BaseConstant.templatesPrefix + "/" + viewName);
    }

    public ModelAndView forwardController(String viewName) {
        return new ModelAndView("forward:/" + viewName);
    }

    public ModelAndView redirectController(String viewName) {
        return new ModelAndView("redirect:/" + viewName);
    }

    /**
     * 公用本地上传
     * @param multipartFile
     * @param uploadPath 完整上传路径
     * @return
     */
    public ResultWapper uploadFile(MultipartFile multipartFile, String uploadPath){

        ResultWapper rw = null;
        do {
            if (multipartFile.isEmpty() || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
                rw = ResultWapper.error(i18n.getMsg("system.error.param.error"));
                break;
            }
            String contentType = multipartFile.getContentType();
            if (!contentType.contains("")) {
                rw = ResultWapper.error(i18n.getMsg("system.error.upload.fileType"));
                break;
            }
            String rootFileName = multipartFile.getOriginalFilename();
            String renameFileName=UUIDGeneratorUtil.getUUID()+"."+rootFileName.substring(rootFileName.lastIndexOf(".")+1);
            if(logger.isDebugEnabled()) {
                logger.debug("上传图片:name={},type={}", rootFileName, contentType);
            }
            User currentUser = userService.getCurrentUser();
            if (currentUser == null) {
                rw = ResultWapper.error(i18n.getMsg("system.error.user.null"));
                break;
            }
            try {
                FileUtil.uploadFile(multipartFile, uploadPath,renameFileName );
                rw = ResultWapper.success(i18n.getMsg("system.success.operate"));
                rw.addResult("imgPath",BaseConstant.nginxFilePrefix+uploadPath.replaceAll(BaseConstant.fileLocation,"")+ File.separator+renameFileName);
            } catch (Exception e) {
                rw = ResultWapper.error(i18n.getMsg("system.error.operate"));
                break;
            }
        } while (false);
        return rw;
    }
}
